package com.spider.moudle.main;

import com.spider.moudle.api.realize.StandardLogRecorder;
import com.spider.moudle.constant.Constant;
import com.spider.moudle.handler.ConsumerHandler;
import com.spider.moudle.handler.ProducerHandler;
import com.spider.moudle.pool.CrawlerPool;
import com.spider.moudle.task.TaskChecker;
import com.spider.moudle.handler.TaskHandler;
import com.spider.moudle.util.CenterExecutorUtil;
import com.spider.moudle.util.KafkaUtil;
import com.spider.moudle.util.PropertiesUtil;
import com.spider.moudle.util.SettingsUtil;

/**
 * Linbo Hu
 */
public class Main {
    /**
     * 初始化操作
     *
     * @throws Exception
     */
    public static void initialize() throws Exception {
        StandardLogRecorder.getInstance().initialize();
        //KafkaUtil.topicCheck();
    }

    public static void main(String[] args) throws Exception {

        //执行pool的构造方法实例化crawler
        CrawlerPool.getInstance().initialize(SettingsUtil.getAllSettingList());
        String role = PropertiesUtil.getVal("spider.project.role");
        String workMode = PropertiesUtil.getVal("spider.work.mode");
        if("PRODUCER".equals(role)) {
            System.out.println("============  当前角色：" + role + "  ============");
        } else {
            System.out.println("============  当前角色：" + role + " || 工作模式：" + workMode + "  ============");
        }

        String runMethod = PropertiesUtil.getVal("run.method");
        String driverType = PropertiesUtil.getVal("browser.driver.type");
        String dataWriteWay = PropertiesUtil.getVal("data.write.way");
        String taskPushWay = PropertiesUtil.getVal("task.push.way");
        String queryMethod = PropertiesUtil.getVal("task.query.method");
        System.out.println("===  浏览器："+ driverType +
                " || 运行方式：" + runMethod+
                " || 查询方式：" + queryMethod
                +" || 任务推送：" + taskPushWay +
                " || 数据推送：" + dataWriteWay +"  ===");


        if("DEVELOP".equals(runMethod)) {
            if("KAFKA".equals(dataWriteWay)) {
                System.err.println("========  开发环境的数据输出建议为文件  ==========");
            }
            if("PRODUCER".equals(role) && "KAFKA".equals(taskPushWay)) {
                System.err.println("========  开发环境任务推送方式建议为本地  ==========");
            }
            if("CONSUMER".equals(role) && "PHANTOMJS".equals(driverType)) {
                System.err.println("========  开发环境浏览器类型建议为Chrome  ==========");
            }
        } else if("OFFICIAL".equals(runMethod)) {
            if("CONSUMER".equals(role)) {
                if("FILE".equals(dataWriteWay)) {
                    System.err.println("========  正式环境的数据输出建议为Kafka  ==========");
                }
            }
            if("PRODUCER".equals(role) && "LOCAL".equals(taskPushWay)) {
                System.err.println("========  正式环境任务推送方式建议为Kafka  ==========");
            }
            if("CONSUMER".equals(role) && "CHROME".equals(driverType)) {
                System.err.println("========  正式环境浏览器类型建议为PHANTOMJS  ==========");
            }
        }

        //初始化操作
        Main.initialize();
        switch (role) {
            case "PRODUCER": {
                //如果是直接查询去重则需要定时清除speech任务池中过期的(两天以前)任务
                if ("LOCAL".equals(queryMethod)) {
                    TaskChecker.getInstance().clear();
                }
                //生成者处理器
                ProducerHandler.handle();
                break;
            }
            case "CONSUMER": {
                //消费者处理器
                ConsumerHandler.handle();
                //定时从kafka拉取任务
                TaskHandler.handle();
                break;
            }
            case "BOTH": {
                //如果是直接查询去重则需要定时清除speech任务池中过期的(两天以前)任务
                if ("LOCAL".equals(queryMethod)) {
                    TaskChecker.getInstance().clear();
                }
                //定时从kafka拉取任务
                if("KAFKA".equals(taskPushWay)) {
                    TaskHandler.handle();
                }
                //生成者处理器
                ProducerHandler.handle();
                //消费者处理器
                ConsumerHandler.handle();
                break;
            }
            default: {
                break;
            }

        }
    }
}
